package com.xuce.nzshopuserweb.controller;

import org.springframework.data.redis.core.RedisTemplate;
import com.xuce.constant.CookieConstant;
import com.xuce.constant.RedisConstant;
import com.xuce.dto.ResultBean;
import com.xuce.nzshopuserweb.client.UserServiceClient;
import com.xuce.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

import javax.servlet.http.Cookie;
@Controller
@RequestMapping("user")
public class LoginController {

    @Autowired
    UserServiceClient userServiceClient;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 验证用户名和密码是否正确
     * @return
     */
    @PostMapping("checkLogin")
    public String checkLogin(String uname,String password
                            , HttpServletResponse response
                    , @CookieValue(name = CookieConstant.USER_CART,required = false)String userCartUuid){

        //交给service去验证用户名和密码是否正确
        ResultBean resultBean = userServiceClient.checkLogin(uname,password,response,userCartUuid);
        if(resultBean.getErrno()==0){
            //登录成功
            //生成cookie
            String uuid = UUID.randomUUID().toString();
            Cookie cookie = new Cookie(CookieConstant.USER_LOGIN,uuid);
            //往redis里存
            //组织键
            String key = StringUtil.getRedisKey(RedisConstant.USER_LOGIN_PRE, uuid);
            //把登录成功后的用户对象存入到redis中。以便checkIsLogin接口去判断是否已登录 来使用
            redisTemplate.opsForValue().set(key,resultBean.getData(),30, TimeUnit.DAYS);
            System.out.println("userWeb resultBean data : "+resultBean.getData() );
            System.out.println("userWeb Redis : "+redisTemplate.opsForValue().get(key));
            //cookie要发送给客户端
            cookie.setMaxAge(30*24*60*60);
            cookie.setPath("/");
            cookie.setHttpOnly(true);
            response.addCookie(cookie);

            //===========合并购物车的Http请求===========
            //因为使用HttpClient来发送请求，所以Cookie不能像浏览器发送请求一样自动携带，因此需要我们手动携带Cookie
            //Cookie: user_cart=3b0c227d-9616-4098-b9f3-71cd1405150e; user_login=92cd6c37-36ea-426c-8a72-a826ae9ef579
//            String url = "http://localhost:9085/cart/merge";
//            StringBuilder sb = new StringBuilder();
//            //未登录的时候如果添加了购物车，那么cookie里就会有这个uuid
//            sb.append(CookieConstant.USER_CART);
//            sb.append("=");
//            sb.append(userCartUuid);
//            sb.append(";");
//            //如果已经登录了，那么cookie里就会有这个user_login的uuid
//            sb.append(CookieConstant.USER_LOGIN);
//            sb.append("=");
//            sb.append(uuid);
//            HttpClientUtils.doGet(url,sb.toString());
            return "redirect:http://localhost:9082";
        }
        return "login";
//       return userServiceClient.checkLogin(uname,password,response,userCartUuid);
    }

    /**
     * 判断用户是否已登录
     */
    @RequestMapping("checkIsLogin")
    @ResponseBody
    public ResultBean checkIsLogin(@CookieValue(name = CookieConstant.USER_LOGIN,required = false) String uuid, HttpServletRequest request){
        return userServiceClient.checkIsLogin(uuid,request);
    }
    /**
     * 注销
     */
    @RequestMapping("logout")
    @ResponseBody
    public ResultBean logout(@CookieValue(name=CookieConstant.USER_LOGIN,required = false) String uuid,
                       HttpServletResponse response){
        return userServiceClient.logout(uuid,response);
    }

}
